<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="50"
					placeholder="用户名/电话"
					
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click.native="startLogin()"
			></wButton>
			
			<!-- 其他登录 -->
			<!-- <view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view> -->
			
			<!-- 底部信息 -->
			<!-- <view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view> -->

			<!-- <view class="footer"> -->
				<!-- <navigator url="forget" open-type="navigate">找回密码</navigator> -->
				<!-- <text>|</text> -->
				<!-- <navigator url="register" open-type="navigate">注册账号</navigator> -->
			<!-- </view> -->
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {webApiURL} from "../index/index.js";
	import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载，也可按需进行加载
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			//登录前清除本地session信息
			uni.removeStorageSync("sessionId");
			this.isLogin();
			dd.ready(function() {
			    dd.runtime.permission.requestAuthCode({
				corpId: 'ding9d240f1d25d65d5635c2f4657eb6378f', // 企业id
				onSuccess: function (info) {
					let code = info.code // 通过该免登授权码可以获取用户身份
					uni.request({
					    url: webApiURL+'/webApi/dingLogin',
						withCredentials:true,
						xhrFields: {
						    withCredentials: true
						},
					    data: {
					        code: code
					    },
					    header: {
					    	'content-type': 'application/x-www-form-urlencoded', 
					    },
					    success: (res) => {
							console.log("请求后台获取钉钉免登授权码: "+JSON.stringify(res.data));
							if(res.data.code == 0){
								uni.showToast({
									icon: 'success',
									position: 'bottom',
									title: '登录成功'
								});
								uni.setStorageSync("sessionId",JSON.stringify(res.data.data.sessionId));
								uni.setStorageSync("userId",JSON.stringify(res.data.data.userId));
								uni.setStorageSync("userName",JSON.stringify(res.data.data.userName));
								uni.reLaunch({
									url: '../index/index',
								});
							}else{
								uni.showToast({
									icon: 'error',
									position: 'bottom',
									title: res.data.msg
								});
							}
					    },
						fail: (res) => {
							console.log("登录异常:"+JSON.stringify(res.data));
						    uni.showToast({
						    	icon: 'success',
						    	position: 'bottom',
						    	title: '系统错误'
						    });
							uni.reLaunch({
								url: '../error/500',
							});
						}
					});
				},
				onFail: function(error){
					console.log('请求后台获取钉钉免登授权码 error: ' + JSON.stringify(error));
				    //失败回调
				}});
				
			});
		},
		methods: {
			isLogin(){
				//判断缓存中是否登录过，直接登录
				// try {
				// 	const value = uni.getStorageSync('setUserData');
				// 	if (value) {
				// 		//有登录信息
				// 		console.log("已登录用户：",value);
				// 		_this.$store.dispatch("setUserData",value); //存入状态
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}
				// } catch (e) {
				// 	// error
				// }
			},
		    startLogin(){
				//登录前清除本地session信息 
				uni.removeStorageSync("sessionId");
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.passData.length < 5) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return;
		        }
				
				console.log("校验成功")
	
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
				uni.showLoading({
					title: '登录中'
				});
				uni.request({
				    url: webApiURL+'/webApi/appLogin',
					method:'POST',
					withCredentials:true,
					xhrFields: {
					    withCredentials: true
					},
				    data: {
				        username: this.phoneData,
				        password: this.passData,
						rememberMe: false
				    },
				    header: {
				    	'content-type': 'application/x-www-form-urlencoded', 
				    },
				    success: (res) => {
						console.log(res.data);
						if(res.data.code == 0){
							uni.showToast({
								icon: 'success',
								position: 'bottom',
								title: '登录成功'
							});
							uni.setStorageSync("sessionId",JSON.stringify(res.data.data.sessionId));
							uni.setStorageSync("userId",JSON.stringify(res.data.data.userId));
							uni.setStorageSync("userName",JSON.stringify(res.data.data.userName));
							setTimeout(function () {
							    uni.reLaunch({
							    	url: '../index/index',
							    });
							}, 1000);
						}else{
							uni.showToast({
								icon: 'error',
								position: 'bottom',
								title: res.data.msg
							});
						}
				        
				    },
					fail: (res) => {
					    uni.showToast({
					    	icon: 'success',
					    	position: 'bottom',
					    	title: '系统错误'
					    });
					}
				});
				
		    },
			/* login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			} */
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
